package com.dearbinge.admin.client.openapi.controller.role;

import com.dearbinge.data.api.admin.AdminSecurityRoleService;
import com.dearbinge.data.pojo.admin.AdminSecurityRole;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Api(tags = "2、角色")
@RestController
@RequestMapping("/role")
@CrossOrigin
@EnableAutoConfiguration
public class RoleController extends WebMvcConfigurerAdapter  {

    @Autowired
    private AdminSecurityRoleService adminSecurityRoleService;


    @ApiOperation(value = "创建或更新角色", notes = "创建或更新角色")
    @RequestMapping(value = "/createOrUpdateUser", method = RequestMethod.POST)
    public ResultMsg<String> createOrUpdateRole(@RequestBody AdminSecurityRole adminSecurityRole) {
    	int data;
        try {
        	if(adminSecurityRole.getId()==0){
        		data = adminSecurityRoleService.insert(adminSecurityRole);
        	}else{
        		data = adminSecurityRoleService.updateByPrimaryKeySelective(adminSecurityRole);
        	}
        	if(data>0){
        		return ResultMsg.ok("ok");
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    @RequestMapping(value = "/selectRoleList", method = RequestMethod.POST)
    public ResultMsg<List<AdminSecurityRole>> selectRoleList() {
        try {
        	return ResultMsg.ok(adminSecurityRoleService.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


}
